package com.mlick.dddk.utils;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static com.mlick.dddk.utils.RootCmd.getIsPower;
import static com.mlick.dddk.utils.RootCmd.openOrClosePower;
import static com.mlick.dddk.utils.RootCmd.runCmd;
import static com.mlick.dddk.utils.RootCmd.sleepTime;
import static com.mlick.dddk.utils.RootCmd.swipePhone;
import static com.mlick.dddk.utils.RootCmd.tapPhone;

/**
 * Created by mlick on 2018-03-22.
 */
public class DKUtils {

    private static boolean isRunning = false;

    public static void rumPro(int flag) throws Exception {

        if (isRunning) {
            SysUtils.out("当前正在进行中!!!");
            return;
        }

        SysUtils.out(String.format("开始执行第(%d)次任务......", flag));

        isRunning = true;

        // 1 判断是否亮屏
        boolean isPower = getIsPower();

        SysUtils.out("检测屏幕是否亮起:" + isPower);
        // 2 如果屏幕暗开屏
        if (!isPower) {
            System.out.print("开启亮屏");
            //            openOrClosePower();
            openOrClosePower();
            sleepTime(3);
            //            TimeUnit.SECONDS.sleep(2);
            // 3 上滑解锁
            System.out.print("上滑解锁");
            swipePhone(365, 810, 365, 200);
            //            TimeUnit.SECONDS.sleep(2);
            sleepTime(3);
        }

        // 4 打开钉钉
        System.out.print("开启钉钉");
        runCmd("am start -n com.alibaba.android.rimet/com.alibaba.android.rimet.biz.SplashActivity");
        //        TimeUnit.SECONDS.sleep(15);
        sleepTime(15);

        //5 进入中心功能
        System.out.print("进入中心功能");
        tapPhone(360, 1250);
        sleepTime(12);

        // 6 进入打开页面
        System.out.print("进入打卡页面");
        //tapPhone(100, 1172);//进入打卡页面
        tapPhone(635, 982);//进入打卡页面
        //        TimeUnit.SECONDS.sleep(10);
        sleepTime(10);

        //        swipePhone(365, 810, 365, 200);
        //        TimeUnit.SECONDS.sleep(2);

        //7 开始打卡
        System.out.print("开始打卡");
        duoCiDaKa(flag);

        // 8 开始截屏
        //System.out.print("开始截屏");
        //String s = getFileName();
        //runCmd("adb shell screencap -p /sdcard/screenshot/" + s);
        //sleepTime(3);
        //
        //System.out.print("获取截屏");
        //runCmd("adb pull /sdcard/screenshot/" + s + " d:/screenshot/");
        //sleepTime(3);

        //当文件
        //if (Files.exists(Paths.get("d:/screenshot/" + s))) {
        //    // 9 发送邮件
        //    System.out.print("发送邮件中");
        //    EmailUtil.sendMail("956217275@qq.com", flag, "d:/screenshot/" + s);
        //    sleepTime(6);
        //}

        // 10 退出 打卡记录
        System.out.print("退出打卡页面");
        //        tapPhone(140, 105); X 按钮
        tapPhone(48, 116); // <-返回按钮
        sleepTime(3);

        // 11 结束 钉钉进程
        System.out.print("结束钉钉进程");
        runCmd("am force-stop com.alibaba.android.rimet");
        sleepTime(1);
        SysUtils.out("关闭屏幕!");
        openOrClosePower();
        SysUtils.out("任务执行完毕!");
        isRunning = false;
    }


    private static void duoCiDaKa(int flag) throws Exception {
        for (int i = 0; i < 5; i++) {//默认五次打卡
            switch (flag) {
                case 1:
                    tapPhone(355, 548);//第一次
                    break;
                case 2:
                    tapPhone(355, 769);//第二次
                    break;
                case 3:
                    tapPhone(355, 990);//第三次
                    break;
                case 4:
                    tapPhone(334, 1150);// 第四次
                    break;
            }
            sleepTime(2, false);
        }
        SysUtils.out("");
    }

    public static void rumProNewThread(final int i) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    rumPro(i);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
